package com.eshop.eshop2021_v1.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eshop.eshop2021_v1.common.JsonResult;
import com.eshop.eshop2021_v1.mapper.before.GoodTypeMapper;
import com.eshop.eshop2021_v1.mapper.before.GoodsMapper;
import com.eshop.eshop2021_v1.mapper.before.UserMapper;
import com.eshop.eshop2021_v1.pojo.GoodType;
import com.eshop.eshop2021_v1.pojo.Goods;
import com.eshop.eshop2021_v1.pojo.User;
import com.eshop.eshop2021_v1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: 若若叶
 * @date: 2021/10/6 9:12 FileName: UserServiceImpl Description:
 */
@Service
public class UserServiceImpl implements UserService {
  @Autowired private UserMapper userMapper;
  @Autowired private GoodsMapper goodsMapper;
  @Autowired private GoodTypeMapper goodTypeMapper;

  @Override
  public JsonResult checkedEmail(String email) {
    // 判断邮箱是否为空
    if (null == email || "".equals(email)) {
      return new JsonResult(201, "邮箱必填", null);
    }
    // qq邮箱正则表达式
    String regex = "[1-9]\\d{7,10}@qq\\.com";
    if (!email.matches(regex)) {
      return new JsonResult(203, "邮箱格式不正确", null);
    }
    // 用QueryWrapper封装参数
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    // 查询数据库“email”一列
    queryWrapper.eq("email", email);
    User user = userMapper.selectOne(queryWrapper);
    // 数据库存在一样邮箱
    if (null != user) {
      return new JsonResult(202, "邮箱被注册", null);
    }
    return new JsonResult(200, "邮箱可用", null);
  }

  @Override
  public JsonResult doReg(
      String email, String password, String comfirmPwd, String code, HttpSession session) {
    // 判断邮箱不为空
    if (null == email || "".equals(email)) {
      return new JsonResult(201, "邮箱不为空", null);
    }
    // 判断密码不为空
    if (null == password || "".equals(password)) {
      return new JsonResult(202, "密码不为空", null);
    }
    // 判断确认密码不为空
    if (null == comfirmPwd || "".equals(comfirmPwd)) {
      return new JsonResult(203, "确认密码不为空", null);
    }

    // 判断验证码不为空
    if (null == code || "".equals(code)) {
      return new JsonResult(204, "验证码不为空", null);
    }

    // 判断密码是否一致
    if (!password.equals(comfirmPwd)) {
      return new JsonResult(205, "密码不一致", null);
    }

    // 获取Httpsession保存的验证码
    String code1 = session.getAttribute("code").toString();
    // 判断验证码是否失效
    if (null == code) {
      return new JsonResult(206, "验证码失效", null);
    }
    // 判断验证码是否正确
    if (!code.equals(code1)) {
      return new JsonResult(207, "验证码有误", null);
    }

    // 数据封装到对象中
    User user = new User(null, email, password, null, null, null, 1);
    int row = userMapper.insert(user);
    // row: 数据库变得行数
    if (row == 1) {
      return new JsonResult(200, "注册成功", null);
    }
    return new JsonResult(208, "注册失败", null);
  }

  @Override
  public String doLogin(User user, String code, HttpSession session, Model model) {
    // 判断邮箱是否为空
    if (null == user.getEmail() || "".equals(user.getEmail())) {
      model.addAttribute("msg", "邮箱必填");
      return "user/login";
    }
    // 判断密码是否为空
    if (null == user.getPassword() || "".equals(user.getPassword())) {
      model.addAttribute("msg", "密码必填");
      return "user/login";
    }
    // 判断验证码是否为空
    if (null == code || "".equals(code)) {
      model.addAttribute("msg", "验证码必填");
      return "user/login";
    }

    // 获取验证码
    String loginCode = session.getAttribute("loginCode").toString();
    // 判断验证码失效
    if (null == loginCode) {
      model.addAttribute("msg", "验证码失效");
      return "user/login";
    }
    // 判断验证码输入有误
    if (!code.toUpperCase().equals(loginCode)) {
      model.addAttribute("msg", "验证码输入有误");
      return "user/login";
    }
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("email", user.getEmail());
    // 判断邮箱是否注册
    User user1 = userMapper.selectOne(queryWrapper);
    if (null == user1) {
      model.addAttribute("msg", "该邮箱未注册，请注册");
      return "user/login";
    }
    // 判断密码
    if (!user1.getPassword().equals(user.getPassword())) {
      model.addAttribute("msg", "登录信息有误");
      return "user/login";
    }

    // 绑定登录用户信息
    session.setAttribute("loginUser", user1);
    // 查询广告商品
    QueryWrapper<Goods> queryWrapper2 = new QueryWrapper<>();
    QueryWrapper<Goods> isAdvertisement = queryWrapper2.eq("isAdvertisement", 1);
    List<Goods> advertisementGoods = goodsMapper.selectList(isAdvertisement);
    model.addAttribute("advertisementGoods", advertisementGoods);
    // 查询商品类型
    QueryWrapper<GoodType> queryWrapper1 = new QueryWrapper<>();
    List<GoodType> goodTypes = goodTypeMapper.selectList(queryWrapper1);
    model.addAttribute("goodTypes", goodTypes);
    // 查询推荐商品
    List<Goods> allRecommendGoods = goodsMapper.selectAllRecommendGoods();
    model.addAttribute("allRecommendGoods", allRecommendGoods);
    // 查询最新商品
    List<Goods> allNewGoods = goodsMapper.selectAllNewGoods();
    model.addAttribute("allNewGoods", allNewGoods);
    return "user/index";
  }

  @Override
  public JsonResult doUpdatePwd(String password, String email) {

    if (null == email || "".equals(email)) {
      return new JsonResult(201, "系统异常", null);
    }
    if (null == password || "".equals(password)) {
      return new JsonResult(202, "请输入密码", null);
    }

    int i = userMapper.doUpdatePwd(password, email);
    if (i == 1) {
      return new JsonResult(200, "修改成功", null);
    } else {
      return new JsonResult(203, "失败了，请重试", null);
    }
  }

  @Override
  public JsonResult updateByEmail(User user) {
    int i = userMapper.updateByEmail(user);
    if (i == 1) {
      return new JsonResult(200, "修改成功", null);
    } else {
      return new JsonResult(201, "出错了，请重试", null);
    }
  }
}
